import os

from PIL import Image
from PyQt5.QtCore import QThread, pyqtSignal


class Convert(QThread):
    # 计算完成信号槽
    finishedSig = pyqtSignal(str)
    # 报错信号槽
    errorSig = pyqtSignal(str)

    def __init__(self, input_folder, output_folder, output_format):
        super(Convert, self).__init__()
        self.input_folder = input_folder
        self.output_folder = output_folder
        self.output_format = output_format

    def run(self) -> None:
        try:
            # 遍历文件夹中的所有文件
            for filename in os.listdir(self.input_folder):
                ext=filename.split('.')[-1]
                if filename.endswith('.png'):
                    # 构建输入和输出文件的路径
                    input_path = os.path.join(self.input_folder, filename)
                    output_path = os.path.join(self.output_folder, filename.replace('.png', '.tiff'))

                    # 打开PNG图像
                    image = Image.open(input_path)

                    # 将图像保存为TIFF格式
                    image.save(output_path, format='TIFF')

                    print(f"转换完成: {output_path}")

            self.finishedSig.emit("转换完成！")
        except Exception as e:
            self.errorSig.emit("转换报错：%s" % e)
